//
//  BaseTableViewController.m
//  联付商圈
//
//  Created by 韩佳睿 on 2017/9/8.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "BaseTableViewController.h"
#import "ODRefreshControl.h"

@interface BaseTableViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) ODRefreshControl *refreshControl;

@end

@implementation BaseTableViewController

- (NSMutableArray *)sectionArray {
    if (_sectionArray) {
        _sectionArray = [@[] mutableCopy];
    }
    return _sectionArray;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [@[] mutableCopy];
    }
    
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)prepareView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
}

- (void)reloadView {
    [self reloadView:NO];
}

- (void)reloadView:(BOOL)cleanUp {
    if (cleanUp) {
        [self.dataArray removeAllObjects];
    }
    
    [self.tableView reloadData];
}

- (void)refreshView
{
    self.pageNum = @1;
    self.lastLoadMoreTime = [NSDate date];
}

- (void)loadMoreView
{
    self.pageNum = @(self.pageNum.integerValue + 1);
}

- (void)setAllowingPullRefreshing:(BOOL)allowingPullRefreshing
{
    [self addPullRefreshScrolling];
}

- (void)addPullRefreshScrolling
{
    self.refreshControl = [[ODRefreshControl alloc] initInScrollView:self.tableView];
    [self.refreshControl addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
}

- (void)dropViewDidBeginRefreshing:(id)sender
{
    [self refreshView];
    
    WEAKSELF
    double delayInSeconds = 3.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [weakSelf.refreshControl endRefreshing];
    });
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.sectionArray.count > 1)
    {
        return self.sectionArray.count;
    }
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.sectionArray.count > 1)
    {
        NSArray *dataArray = self.dataArray[section];
        
        return dataArray.count;
    }
    
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

@end
